﻿Imports System.Configuration
Imports System.IO

Namespace Common

    ''' <summary>
    ''' 配置文件基类(2011/07/29)
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ConfigBase
        Implements IDisposable

#Region "字段"
        Private Shared _config As System.Configuration.Configuration
        Private Shared _myconfig As ConfigBase
#End Region

#Region "构造函数"

        ''' <summary>
        ''' 获取到配置文件对象
        ''' </summary>      
        Public Shared Property Instance() As ConfigBase
            Get
                If _config Is Nothing Then
                    _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                    If CreateAppConfig(_config.FilePath) = True Then
                        _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                    End If
                End If

                If _myconfig Is Nothing Then
                    _myconfig = New ConfigBase
                End If

                Return _myconfig
            End Get
            Set(ByVal value As ConfigBase)
                _myconfig = value
            End Set
        End Property

        ''' <summary>
        ''' 获取到配置文件对象
        ''' </summary>   
        ''' <param name="filePath">文件路径</param>        
        Public Shared Property Instance(ByVal filePath As String) As ConfigBase
            Get
                If _config Is Nothing Then
                    _config = ConfigurationManager.OpenExeConfiguration(filePath)
                    If CreateAppConfig(filePath) = True Then
                        _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                    End If
                End If

                If _myconfig Is Nothing Then
                    _myconfig = New ConfigBase()
                End If

                Return _myconfig

            End Get
            Set(ByVal value As ConfigBase)
                _myconfig = value
            End Set
        End Property
#End Region

#Region "公开方法"

        ''' <summary>
        ''' 设置接点的值
        ''' </summary>
        ''' <param name="name">键</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetAppSectionValue(ByVal name As String) As String

            Return _config.AppSettings.Settings.Item(name).Value

        End Function

        ''' <summary>
        ''' 更新键值
        ''' </summary>
        ''' <param name="name">键</param>
        ''' <param name="value">值</param>
        ''' <returns></returns>
        ''' <remarks>不存在的自动添加</remarks>
        Public Function SetAppSectionValue(ByVal name As String, ByVal value As String) As Boolean

            Dim bExist As Boolean = False

            For Each strname As String In _config.AppSettings.Settings.AllKeys
                If strname.ToLower = name.ToLower Then
                    bExist = True
                    Exit For
                End If
            Next

            If bExist Then
                _config.AppSettings.Settings.Item(name).Value = value
                _config.Save()
            Else
                Dim kv As New System.Configuration.KeyValueConfigurationElement(name, value)
                _config.AppSettings.Settings.Add(kv)
                _config.Save()
            End If

        End Function

        ''' <summary>
        ''' 删除键值对
        ''' </summary>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function DeleteAppSection(ByVal key As String) As Boolean
            Dim bExist As Boolean = False
            For Each strname As String In _config.AppSettings.Settings.AllKeys
                If strname.ToLower = key.ToLower Then
                    bExist = True
                    Exit For
                End If
            Next

            If bExist Then
                _config.AppSettings.Settings.Remove(key)
                _config.Save()
            End If
        End Function

#End Region

#Region "垃圾回收"
        Private disposedValue As Boolean = False        ' 检测冗余的调用

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: 释放其他状态(托管对象)。
                End If

                If _config IsNot Nothing Then
                    _config = Nothing
                End If
                ' TODO: 释放您自己的状态(非托管对象)。
                ' TODO: 将大型字段设置为 null。
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' Visual Basic 添加此代码是为了正确实现可处置模式。
        Public Sub Dispose() Implements IDisposable.Dispose
            ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

#End Region

#End Region

#Region "私有方法"
        Private Shared Function CreateAppConfig(ByVal path As String) As Boolean

            If Not System.IO.File.Exists(_config.FilePath) Then
                If _defaultContent.Trim = "" Then
                    Dim sb As New System.Text.StringBuilder
                    sb.AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")
                    sb.AppendLine("<configuration>")
                    sb.AppendLine("  <appSettings>")
                    sb.AppendLine("  </appSettings>")
                    sb.AppendLine("</configuration>")
                    _defaultContent = sb.ToString
                End If
                File.WriteAllText(_config.FilePath, _defaultContent)
                Return True
            End If

            Return False

        End Function
#End Region

#Region "配置文件不存在默认内容"
        Private Shared _defaultContent As String = ""
        Public Shared Property DefaultContent() As String
            Get
                Return _defaultContent
            End Get
            Set(ByVal value As String)
                _defaultContent = value
            End Set
        End Property
#End Region

    End Class
End Namespace


